<?php

namespace App\Http\Controllers;

use App\Models\Reply;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests\ReplyRequest;
use Auth;

class RepliesController extends Controller
{
    public function __construct()
    {
        $this -> middleware('auth');
    }

    /**
     * 保存话题回复
     */
    public function store(ReplyRequest $request, Reply $reply)
    {
        $reply->content = $request->content;
        $reply->user_id = Auth::id();
        $reply->topic_id = $request->topic_id;
        $reply->save();

        return redirect() -> to($reply->topic -> link()) -> with('success', '创建成功！');
    }

    /**
     * 删除话题回复
     */
    public function destroy(Reply $reply)
    {
        // 此处 destroy 为 ReplyPolicy 里面的 destroy
        $this -> authorize('destroy', $reply);
        $reply -> delete();

        return redirect() -> to($reply->topic->link()) -> with('success', '删除成功！');
    }
}